<?php

namespace Services\Api;

use Common\Models\FUsersExtend;
use Services\ServicesApiBase;

class TurnServices extends ServicesApiBase
{

    /**
     * 商品转链
     * @param $user_info 用户信息
     * @param $site 平台
     * @param $goods_id 商品id
     * @param $couponurl 优惠价链接
     * @param string $source 终端平台
     */
    public function goods_turn($site, $user_info, $goods_id, $couponurl, $source = "android")
    {
        $responseReturn = ['status' => false, 'msg' => '转链失败', 'data' => []];
        $FUserExtendsDb = new FUsersExtend();
        switch ($site) {
            case 'haodanku':
                if (empty($user_info['tb_rid'])) {
                    $responseReturn['msg'] = "请先淘宝授权";
                    return $responseReturn;
                }
                $res = $this->zhetaoke->taobao_turn($user_info['tb_rid'], $goods_id, $source, $user_info['tb_special_id']);      //商品转链
                if (!$res['status']) {
                    $responseReturn['msg'] = $res['msg'];
                    return $responseReturn;
                }
                break;
            case 'jd':
                if (empty($user_info['jd_rid'])) {
                    $FUserExtendsDb->save_columns($user_info['id'], 'jd_rid', $user_info['phone']);
                }
                $res = $this->jtt->get_goods_link($user_info['phone'], $goods_id, $couponurl);
                if (!$res['status']) {
                    $responseReturn['msg'] = $res['msg'];
                    return $responseReturn;
                }
                break;
            case 'pdd':
                if (empty($user_info['pdd_pid'])) {
                    $res = $this->pdd->Createppdid($user_info['phone']);
                    if (!$res['status']) {
                        $responseReturn['msg'] = $res['msg'];
                        return $responseReturn;
                    }
                    $FUserExtendsDb->save_columns($this->user_info['id'], 'pdd_pid', $res['data']['pid']);
                    $user_info['pdd_pid'] = $res['data']['pid'];
                }
                $res = $this->pdd->pdd_turn($goods_id, $user_info['pdd_pid']);
                if (!$res['status']) {
                    $responseReturn['msg'] = $res['msg'];
                    return $responseReturn;
                }
                break;
            case 'vph':
                if (empty($user_info['vph_rid'])) {
                    if (empty($user_info['vph_rid'])) {
                        $FUserExtendsDb->save_columns($user_info['id'], 'vph_rid', $user_info['id']);
                    }
                }
                $res = $this->vph->createturn($user_info['id'], $goods_id);
                if (!$res['status']) {
                    $responseReturn['msg'] = $res['msg'];
                }
                break;
            case 'suning':
                if (empty($user_info['sn_rid'])) {
                    $responseMsg = $this->suning->promotion_create($user_info['phone']);
                    $FUserExtendsDb->save_columns($user_info['id'], 'sn_rid', $responseMsg['data']['pid']);
                    $user_info['sn_rid'] = $responseMsg['data']['pid'];
                }
                $goodsId = explode('-', $goods_id);
                $res = $this->suning->turnHoistinglink($user_info['sn_rid'], $goodsId[0], $goodsId[1]);
                if (!$res['status']) {
                    $responseMsg['msg'] = $res['msg'];
                }
                break;
            case 'douyin':
                if (empty($user_info['dy_rid'])) {
                    $FUserExtendsDb->save_columns($user_info['id'], 'dy_rid', "dy_{$user_info['id']}");
                    $user_info['dy_rid'] = "dy_{$user_info['id']}";
                }
                $res = $this->haodanku->douyinTurnLink($user_info['dy_rid'], $goods_id);
                if (!$res['status']) {
                    $responseMsg['msg'] = $res['msg'];
                }
                break;
        }
        $responseReturn['status'] = true;
        $responseReturn['msg'] = "获取成功";
        $responseReturn['data'] = $res['data'];
        return $responseReturn;
    }


    /**
     * 活动转链
     * @param $site 平台
     * @param $user_info 用户信息
     * @param $activity_source 资源信息
     * @param string $source 终端平台
     */
    public function activity_turnlink($site, $user_info, $activity_source, $source = "android")
    {
        $turnLinkResponse = ['status' => false, 'data' => [], 'msg' => ''];
        $FUserExtendsDb = new FUsersExtend();
        switch ($site) {
            case 'taobao':
                if (empty($user_info['tb_rid'])) {
                    $turnLinkResponse['msg'] = "请先淘宝授权";
                    return $turnLinkResponse;
                }
                $res = $this->taobao->activitylink_new($activity_source, $user_info['tb_rid'], $source);
                break;
            case 'jd':
                if (empty($user_info['jd_rid'])) {
                    $FUserExtendsDb->save_columns($user_info['id'], 'jd_rid', $user_info['phone']);
                    $user_info['jd_rid'] = $user_info['id'];
                }
                $res = $this->jtt->get_goods_link($user_info['phone'], $activity_source);
                break;
            case 'pdd':
                if (empty($user_info['pdd_pid'])) {
                    $res = $this->pdd->Createppdid($user_info['phone']);
                    if (!$res['status']) {
                        $responseReturn['msg'] = $res['msg'];
                        return $responseReturn;
                    }
                    $FUserExtendsDb->save_columns($user_info['id'], 'pdd_pid', $res['data']['pid']);
                    $user_info['pdd_pid'] = $res['data']['pid'];
                }
                $res = $this->pdd->site_turn($user_info['pdd_pid'], '', $activity_source);
                break;
            case 'vph':
                if (empty($user_info['vph_rid'])) {
                    if (empty($user_info['vph_rid'])) {
                        $FUserExtendsDb->save_columns($user_info['id'], 'vph_rid', $user_info['id']);
                        $user_info['vph_rid'] = $user_info['id'];
                    }
                }
                $res = $this->vph->activityTurnlink($user_info['id'], $activity_source);
                break;
            case 'suning':
                if (empty($user_info['sn_rid'])) {
                    $responseMsg = $this->suning->promotion_create($user_info['phone']);
                    $FUserExtendsDb->save_columns($user_info['id'], 'sn_rid', $responseMsg['data']['pid']);
                    $user_info['sn_rid'] = $responseMsg['data']['pid'];
                }
                $res = $this->suning->urlQueryRequest($user_info['sn_rid'], $activity_source);
                break;
            case 'jtk':
                if (empty($user_info['jtk_rid'])) {
                    $FUserExtendsDb->save_columns($user_info['id'], 'jtk_rid', $user_info['id']);
                    $user_info['jtk_rid'] = $user_info['id'];
                }
                $res = $this->jutuike->activityTurnlink($user_info['id'], $activity_source);
                break;
        }
        if (!$res['status']) {
            $turnLinkResponse['msg'] = $res['msg'];
            return $turnLinkResponse;
        }
        $turnLinkResponse['status'] = true;
        $turnLinkResponse['data'] = $res['data'];
        $turnLinkResponse['msg'] = '获取成功';
        return $turnLinkResponse;
    }


    /**
     * 拼多多备案查询
     */
    public function pdd_keep_on_record($user_info)
    {
        if ($user_info['pdd_pid']) {
            $res = $this->pdd->member_authority($user_info['id'], $user_info['pdd_pid']);
            if ($res) {
                if ($user_info['pdd_auth'] != '1') {
                    $FUserExtendsDb = new FUsersExtend();
                    $FUserExtendsDb->save_columns($user_info['id'], 'pdd_auth', 1);
                }
                $result['h5url'] = "";
                $result['appurl'] = "";
                return ['status' => true, 'msg' => '推广位已经备案', 'data' => $result];
            }
        }
        if (empty($user_info['pdd_pid'])) {
            $responseMsg = $this->pdd->Createppdid($user_info['phone']);     //当前的电话号码如果没有生成过推广位，则使用这个电话，否则使用下面那个
            if ($responseMsg['status']) {
                $FUserExtendsDb = new FUsersExtend();
                $FUserExtendsDb->save_columns($user_info['id'], 'pdd_pid', $responseMsg['data']['pid']);
                $user_info['pdd_pid'] = $responseMsg['data']['pid'];
            } else {
                return ['status' => false, 'msg' => '他推广位创建失败'];
            }
        }
        $responseReturn = $this->pdd->rp_prom($user_info['id'], $user_info['pdd_pid']);
        return $responseReturn;

    }
}